/**
 * FileUtil.java - 2012-6-4
 *
 * Licensed Property to China UnionPay Co., Ltd.
 * 
 * (C) Copyright of China UnionPay Co., Ltd. 2010
 *     All Rights Reserved.
 *
 * Project Info: China UnionPay Internet Acquiring Project
 * 
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *        
 * =============================================================================
 */
package com.lijin.study.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;

/**
 * Description: 
 *
 * (C) Copyright of China UnionPay Co., Ltd. 2010.
 *
 */
public class FileUtil {
	
	public static void readFileByLine(File f, FileLineProcessor flp, String charset, int startLineNo) throws Exception {
		if (charset == null || "".equals(charset.trim())) {
			charset = "UTF-8";
		}
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), charset));
		int c = 0;
		String line = null;
		while (reader.ready()) {
			c ++;
			line = reader.readLine();
			if (c < startLineNo) {
				continue;
			}
			flp.processLine(line);
		}
		reader.close();
	}
	
	public static void writeFileByLine(File f, List<String> contents, String charset) throws Exception {
		if (charset == null || "".equals(charset.trim())) {
			charset = "UTF-8";
		}
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), charset));
		for (String line : contents) {
			writer.write(line);
			writer.newLine();
		}
		writer.close();
	}
}
